// https://learn.microsoft.com/en-us/typography/opentype/spec/hhea
// This table contains information for horizontal layout.
// The values in the minRightSidebearing, minLeftSideBearing and xMaxExtent should be computed using only glyphs that have contours.
// Glyphs with no contours should be ignored for the purposes of these calculations.
// All reserved areas must be set to 0.

// The ascender, descender and linegap values in this table are Apple specific; see Apple's specification for details regarding Apple platforms.
// The sTypoAscender, sTypoDescender and sTypoLineGap fields in the OS/2 table are used on the Windows platform, and are recommended for new text-layout implementations.
// Font developers should evaluate behavior in target applications that may use fields in this table or in the OS/2 table to ensure consistent layout.
// See the descriptions of the OS/2 fields for additional details.

// In a variable font, various font-metric values within the horizontal header table may need to be adjusted for different variation instances.
// Variation data for 'hhea' entries can be provided in the metrics variations (MVAR) table.
// Different 'hhea' entries are associated with particular variation data in the MVAR table using value tags, as follows:
// 'hhea' entry    Tag
// caretOffset     'hcof'
// caretSlopeRise  'hcrs'
// caretSlopeRun   'hcrn'

use super::*;

#[derive(Debug, Clone)]
pub struct Table {
	pub major_version: u16,
	pub minor_version: u16,
	pub ascender: FWORD,               // Typographic ascent
	pub descender: FWORD,              // Typographic descent
	pub line_gap: FWORD, // Typographic line gap. Negative lineGap values are treated as zero in some legacy platform implementations.
	pub advance_width_max: UFWORD, // Maximum advance width value in 'hmtx' table.
	pub min_left_side_bearing: FWORD, // Minimum left sidebearing value in 'hmtx' table for glyphs with contours (empty glyphs should be ignored).
	pub min_right_side_bearing: FWORD, // Minimum right sidebearing value; calculated as min(aw - (lsb + xMax - xMin)) for glyphs with contours (empty glyphs should be ignored).
	pub x_max_extent: FWORD,           // Max(lsb + (xMax - xMin)).
	pub caret_slope_rise: i16,         // Used to calculate the slope of the cursor (rise/run); 1 for vertical.
	pub caret_slope_run: i16,          // 0 for vertical.
	pub caret_offset: i16, // The amount by which a slanted highlight on a glyph needs to be shifted to produce the best appearance. Set to 0 for non-slanted fonts
	pub reserved0: i16,    // set to 0
	pub reserved1: i16,    // set to 0
	pub reserved2: i16,    // set to 0
	pub reserved3: i16,    // set to 0
	pub metric_data_format: i16, // 0 for current format.
	pub num_metrics: u16, // Number of hMetric entries in 'hmtx' table
}

impl Table {
	pub fn parse(data: &[u8]) -> Option<Self> {
		let mut s = Stream::new(data);
		let this = Self {
			major_version: s.read::<u16>()?,
			minor_version: s.read::<u16>()?,
			ascender: s.read::<FWORD>()?,
			descender: s.read::<FWORD>()?,
			line_gap: s.read::<FWORD>()?,
			advance_width_max: s.read::<UFWORD>()?,
			min_left_side_bearing: s.read::<FWORD>()?,
			min_right_side_bearing: s.read::<FWORD>()?,
			x_max_extent: s.read::<FWORD>()?,
			caret_slope_rise: s.read::<i16>()?,
			caret_slope_run: s.read::<i16>()?,
			caret_offset: s.read::<i16>()?,
			reserved0: s.read::<i16>()?,
			reserved1: s.read::<i16>()?,
			reserved2: s.read::<i16>()?,
			reserved3: s.read::<i16>()?,
			metric_data_format: s.read::<i16>()?,
			num_metrics: s.read::<u16>()?,
		};
		Some(this)
	}
}
